package com.ext.aic.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.ext.aic.AICManager;
import com.ext.aic.R;
import com.ext.aic.beans.WhiteListItem;
import com.ext.aic.constants.EBusConstants;
import com.ext.aic.databinding.AddWhiteListLayoutBinding;
import com.ext.aic.utils.KeyboardUtils;
import com.ext.aic.utils.ToastyUtils;
import com.ext.aic.utils.WhiteListUtils;
import com.eyinfo.android_pure_utils.ebus.EBus;
import com.eyinfo.android_pure_utils.snow.Butterfly;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;

public class AddWhiteListActivity extends BaseActivity {

    private AddWhiteListLayoutBinding binding;

    @NotEmpty(message = "请输入微信用户")
    private EditText userEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(this, R.layout.add_white_list_layout, null);
        binding = AddWhiteListLayoutBinding.bind(view);
        setContentView(binding.getRoot());
        bindView();
        bindData();
    }

    private void bindView() {
        binding.topView.backIv.setOnClickListener(v -> {
            KeyboardUtils.hideKeyboard(this);
            finish();
        });
        binding.topView.titleTv.setText(R.string.white_list_edit);
        binding.submitBtn.setOnClickListener(this::onSubmitClick);
        userEt = binding.userEt;
        binding.userEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                binding.submitBtn.setEnabled(!TextUtils.isEmpty(s.toString()));
            }
        });
    }

    private void bindData() {
        WhiteListItem whiteList = getSerializable("whiteList", WhiteListItem.class);
        if (whiteList != null && whiteList.getId() > 0) {
            binding.userEt.setText(whiteList.getUser());
        }
    }

    public void onSubmitClick(View view) {
        KeyboardUtils.hideKeyboard(this);
        validatorHandler.validator.validate();
    }

    @Override
    public void onValidationSucceeded() {
        String user = binding.userEt.getText().toString();
        WhiteListItem item = WhiteListUtils.getInstance().findWhiteList(user);
        if (item == null || item.getId() == 0) {
            item = new WhiteListItem();
            item.setId(Butterfly.getInstance().nextId());
        }
        item.setUser(user);
        AICManager.getInstance().insertOrUpdate(WhiteListItem.class, item);
        finish();
        ToastyUtils.getInstance().show(getString(R.string.save_success));
        EBus.getInstance().post(EBusConstants.refreshWhileList);
    }
}
